package com.gxa.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.gxa.common.R;
import com.gxa.entity.*;
import com.gxa.mapper.ClosePatientMapper;
import com.gxa.service.ClosePatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ClosePatientServiceImpl implements ClosePatientService {
    @Autowired
    private ClosePatientMapper closePatientMapper;
    R r = new R();
    DataLog dataLog = new DataLog();
    SysActionLog sysActionLog = new SysActionLog();

    /**
     * 1分页查询所有的密集人员接口
     * @param currentPage
     * @param pageSize
     */
    @Override
    public R findClosePatientList(Integer currentPage, Integer pageSize) {

        Page page = PageHelper.startPage(currentPage,pageSize);

        List<DistrictSysContact> districtSysContactList = closePatientMapper.findClosePatientList();

        int total =  (int)page.getTotal();

        Map map =new HashMap();

        map.put("total",total);
        map.put("districtSysContactList",districtSysContactList);


        r.setCode(200);
        r.setMsg("查询成功");
        r.setData(map);

        return r;
    }

    /**
     * 2条件查询密集人员接口
     * @param currentPage
     * @param pageSize
     */
    @Override
    public R findClosePatientListByConditions(Integer currentPage, Integer pageSize, String name, String sex, String idNum, String phoneNum, String address, String registrant, Date registrationDate, String jointType, String status) {

        Page page = PageHelper.startPage(currentPage,pageSize);

        List<DistrictSysContact> districtSysContactList = closePatientMapper.findClosePatientListByConditions(name,sex,idNum,phoneNum,address,registrant,registrationDate,jointType,status);

        int total = (int) page.getTotal();

        Map map = new HashMap();

        map.put("total",total);
        map.put("districtSysContactList",districtSysContactList);

        r.setCode(200);
        r.setMsg("查询成功");
        r.setData(map);

        return r;
    }

    /**
     * 3根据ID查询密接人员
     * @param id
     * @return
     */
    @Override
    public R findClosePatientById(Integer id) {
        DistrictSysContact districtSysContact = closePatientMapper.findClosePatientById(id);
        districtSysContact.setPatientVaccinationList(closePatientMapper.findPatientVaccinationByPatientId(districtSysContact.getId()));

        r.setCode(200);
        r.setMsg("查询成功");
        r.setData(districtSysContact);
        return r;
    }

    /**
     * 4新增密接人员，次密接人员，一般接触人员
     * @param districtSysContact
     * @return
     */
    @Override
    @Transactional
    public R addClosePatient(DistrictSysContact districtSysContact,String JointType,String operator) {

            districtSysContact.setJointType(JointType);
            districtSysContact.setRegistrant(operator);
            districtSysContact.setRegistrationDate(new Date());
            closePatientMapper.addClosePatient(districtSysContact);

        for (PatientVaccination patientVaccination :districtSysContact.getPatientVaccinationList()){
            closePatientMapper.addPatientVaccination(patientVaccination);
            Map map = new HashMap();
            map.put("patientid",districtSysContact.getId());
            map.put("vaccinationid",patientVaccination.getVacId());
            map.put("patienttype",JointType);
            closePatientMapper.addPatientidVaccinationid(map);
        }

            //添加数据日志
            dataLog.setPatientId(districtSysContact.getId());
            dataLog.setOperationContent("登记了"+JointType+"者"+districtSysContact.getName()+"的信息");
            dataLog.setOperator(operator);
            dataLog.setOperationTime(new Date());
            closePatientMapper.addDataLog(dataLog);

            //添加系统日志
            String organization = closePatientMapper.findOrganizationByUserName(operator);
            sysActionLog.setOrganization(organization);
            sysActionLog.setOperationTime(new Date());
            sysActionLog.setOperator(operator);
            sysActionLog.setOperationContent("新增了"+JointType+"患者");

            closePatientMapper.addSysActionLog(sysActionLog);

            r.setCode(200);
            r.setMsg("添加成功");

        return r;
    }

    /**
     * 7编辑密接人员信息
     * @param districtSysContact
     * @param operator
     * @return
     */
    @Override
    @Transactional
    public R updateClosePatient(DistrictSysContact districtSysContact, String operator) {
        closePatientMapper.updateClosePatient(districtSysContact);
        for (PatientVaccination patientVaccination :districtSysContact.getPatientVaccinationList()){
            closePatientMapper.updatePatientVaccination(patientVaccination);
        }
        dataLog.setPatientId(districtSysContact.getId());
        dataLog.setOperationContent("更新了"+districtSysContact.getJointType()+"者"+districtSysContact.getName()+"的信息");
        dataLog.setOperator(operator);
        dataLog.setOperationTime(new Date());
        closePatientMapper.addDataLog(dataLog);

        r.setCode(200);
        r.setMsg("修改成功");

        return r;
    }

    /**
     * 8删除密接人员的接口
     * @param id
     * @return
     */
    @Override
    @Transactional
    public R delCrowded(int id) {
        //删除基本信息
        closePatientMapper.delClosePatient(id);
        //删除流行病学轨迹调查
        closePatientMapper.deldiseaseSurvey(id);
        //删除疫苗接种记录
        closePatientMapper.delPatientVaccination(id);
        //删除中间表
        closePatientMapper.delPatientidVaccinationid(id);
        //删除跟踪转运
        r.setCode(200);
        r.setMsg("删除成功");
        return r;
    }

    /***
     * 9查看基本信息接口
     * @param id
     * @return
     */
    @Override
    public R selectBasicInfo(int id) {

        DistrictSysContact districtSysContact = closePatientMapper.selectBasicInfo(id);

        String confirmedPatientName =closePatientMapper.findConfirmedPatientNameById(districtSysContact.getAssociatedCases());


        Map map = new HashMap();
        map.put("districtSysContact",districtSysContact);
        map.put("confirmedPatientName",confirmedPatientName);
        r.setCode(200);
        r.setMsg("查询成功");
        r.setData(map);
        return r;
    }
    /**
     * 10查看流行病学轨迹调查接口
     * @param id
     * @return
     */
    @Override
    public R finddiseaseSurvey(int id) {
        r.setCode(200);
        r.setMsg("查询成功");
        r.setData(closePatientMapper.finddiseaseSurvey(id));
        return r;
    }
    /**
     * 11根据ID查询轨迹调查
     * @param id
     * @return
     */
    @Override
    public R finddiseaseSurveyById(Integer id) {
        r.setCode(200);
        r.setMsg("查询成功");
        r.setData(closePatientMapper.finddiseaseSurveyById(id));
        return r;
    }
    /**
     * 12添加轨迹调查
     * @param patientTrack
     * @param patientId
     * @param operator
     * @return
     */
    @Override
    @Transactional
    public R addDiseaseSurvey(PatientTrack patientTrack,int patientId,String operator) {
        patientTrack.setPatientId(patientId);
        closePatientMapper.addDiseaseSurvey(patientTrack);
        DistrictSysContact districtSysContact = closePatientMapper.findClosePatientById(patientId);
        dataLog.setPatientId(patientId);
        dataLog.setOperationContent("登记了"+districtSysContact.getJointType()+"者"+districtSysContact.getName()+"的信息");
        dataLog.setOperator(operator);
        dataLog.setOperationTime(new Date());
        closePatientMapper.addDataLog(dataLog);

        r.setCode(200);
        r.setMsg("添加成功");

        return r;
    }

    /**
     * 13修改轨迹调查
     * @param patientTrack
     * @param operator
     * @return
     */
    @Override
    @Transactional
    public R updateDiseaseSurvey(PatientTrack patientTrack,String operator) {
        closePatientMapper.updateDiseaseSurvey(patientTrack);
        DistrictSysContact districtSysContact = closePatientMapper.findClosePatientById(patientTrack.getId());
        dataLog.setPatientId(districtSysContact.getId());
        dataLog.setOperationContent("更新了"+districtSysContact.getJointType()+"者"+districtSysContact.getName()+"的信息");
        dataLog.setOperator(operator);
        dataLog.setOperationTime(new Date());
        closePatientMapper.addDataLog(dataLog);
        r.setCode(200);
        r.setMsg("修改成功");

        return r;
    }
    /**
     * 14查询管理次密接人员信息
     * @param currentPage
     * @param pageSize
     * @param id
     * @return
     */
    @Override
    public R selectNextCrowded(Integer currentPage, Integer pageSize,Integer id) {
        Page page = PageHelper.startPage(currentPage,pageSize);

        List<DistrictSysContact> districtSysContactList = closePatientMapper.selectNextCrowded(id);

        int total =  (int)page.getTotal();

        Map map =new HashMap();

        map.put("total",total);
        map.put("districtSysContactList",districtSysContactList);


        r.setCode(200);
        r.setMsg("查询成功");
        r.setData(map);

        return r;
    }

    /**
     * 15根据条件查询次密接列表
     * @param currentPage
     * @param pageSize
     * @param id
     * @param name
     * @param sex
     * @param idNum
     * @param phoneNum
     * @return
     */
    @Override
    public R selectNextCrowdedByConditions(Integer currentPage, Integer pageSize, Integer id, String name, String sex, String idNum, String phoneNum) {

        Page page = PageHelper.startPage(currentPage,pageSize);

        List<DistrictSysContact> districtSysContactList = closePatientMapper.selectNextCrowdedByConditions(id,name,sex,idNum,phoneNum);

        int total =  (int)page.getTotal();

        Map map =new HashMap();

        map.put("total",total);
        map.put("districtSysContactList",districtSysContactList);


        r.setCode(200);
        r.setMsg("查询成功");
        r.setData(map);

        return r;
    }

    /**
     * 16查看跟踪转运-转运记录接口
     * @param id
     * @return
     */
    @Override
    public R selectTrack(int id) {
        r.setCode(200);
        r.setMsg("查询成功");
        r.setData(closePatientMapper.selectTrack(id));
        return r;
    }
    /**
     * 17查看跟踪转运-隔离接收记录
     * @param id
     * @return
     */
    @Override
    public R selectIsolationReceivingRecord(int id) {
        r.setCode(200);
        r.setMsg("查询成功");
        r.setData(closePatientMapper.selectIsolationReceivingRecord(id));
        return r;
    }
    /**
     * 18查看跟踪转运-采样检测记录
     * @param id
     * @return
     */
    @Override
    public R selectPatientSample(int id) {
        r.setCode(200);
        r.setMsg("查询成功");
        r.setData(closePatientMapper.selectPatientSample(id));
        return r;
    }
    /**
     * 19查看跟踪转运-解除隔离记录接口
     * @param id
     * @return
     */
    @Override
    public R selectDeisolationRecord(int id) {
        r.setCode(200);
        r.setMsg("查询成功");
        r.setData(closePatientMapper.selectDeisolationRecord(id));
        return r;
    }
    /**
     * 20查看跟踪转运-跟踪转运日志
     * @param id
     * @return
     */
    @Override
    public R selectTrackTransshipmentLog(int id) {
        r.setCode(200);
        r.setMsg("查询成功");
        r.setData(closePatientMapper.selectTrackTransshipmentLog(id));
        return r;
    }
    /**
     * 21数据日志
     * @param id
     * @return
     */
    @Override
    public R dataLog(int id) {
        r.setCode(200);
        r.setMsg("查询成功");
        r.setData(closePatientMapper.finddataLogs(id));
        return r;
    }

}
